package com.apestech.formwork.run.core;

import com.apestech.formwork.xml.core.NodeVar;
import com.apestech.formwork.xml.core.exec.SingleNode;
import com.apestech.formwork.xml.core.var.NodeVarExcute;
import com.apestech.framework.db.SqlSession;
import com.apestech.framework.json.JSONArray;
import com.apestech.framework.json.JSONObject;

/**
 * 循环方式处理ROW行数据处理
 *
 * @author hup
 * @create 2018-04-28-11:30
 */
public class ExecWhileRow {

    private SingleNode node;
    private JSONObject data;
    private JSONObject input;
    private JSONObject output;
    private boolean success=true;
    private SqlSession session;
    private String error;
    private String errorId;
    private JSONArray outList;

    public ExecWhileRow(SingleNode node){
        this.node=node;
    }

    public void setData(JSONObject data){
        this.data=data;
    }

    public void setSqlSession(SqlSession session){
        this.session=session;
    }

    public void expain() throws Exception {
        this.input=ExecUtil.getInput(node, data, this.session);
        JSONArray list= (JSONArray) ExecUtil.execProcess(node.getParam(), this.input, this, this.session);
        String type=this.node.getType();
        int wlen=this.node.getWhileList().size();
        int len=list.size();
        if(len>0){
            if(ExecUtil.hasOutput(this.node)){
                this.output=ExecUtil.getOutput(this.node, data, this.session);
            } else {
                if(wlen==0){
                    this.success=false;
                    this.errorId="WHILE_ROW_ERROR";
                    this.error="没有符合条件的输入数据！";
                }
                this.outList=new JSONArray();
                if(NodeVar.getROW().equals(type)){
                    if(wlen>0){
                        for(int i=0; i<len; i++){
                            JSONObject map= (JSONObject) list.get(i);
                            for(int w=0; w<wlen; w++){
                                SingleNode wnode=this.node.getWhileList().get(w);
                                ExecWhileRow execRow=new ExecWhileRow(wnode);
                                execRow.setSqlSession(this.session);
                                JSONObject m=new JSONObject(map);
                                m.putAll(this.data);
                                execRow.setData(m);
                                execRow.expain();
                                if(!execRow.success){
                                    this.success=false;
                                    this.errorId=execRow.getErrorId();
                                    this.error=execRow.getError();
                                    return;
                                } else {
                                    this.outList.add(execRow.getOutput());
                                }
                            }
                            if(NodeVarExcute.getPRIORITY().equals(this.node.getName())){
                                break;
                            }
                        }
                    }
                }
            }
        }
        if(len<=0){
            this.success=false;
            this.errorId="WHILE_ROW_ERROR";
            this.error="没有符合条件的输入数据！";
        }
    }

    public JSONObject getOutput() {
        return output;
    }

    public boolean isSuccess() {
        return success;
    }

    public SqlSession getSession() {
        return session;
    }

    public String getError() {
        return error;
    }

    public String getErrorId() {
        return errorId;
    }

    public JSONArray getOutList() {
        return outList;
    }
}
